<?
require_once("header.misc.php");

class PeopleGroup {
	
	public $info;
	
	function __construct($gid) {
		connectToDB();
		if(!is_numeric($gid)) {
			$gid=getGidByName($gid);
		}
		if(!is_numeric($gid)) {
			return false;
		}
		$this->info = groupInfo($gid);
		if(!$this->info) {
			echo "OH DARN!";
			return false;
		}
	}
	
	public function changeName($newname) {
		connectToDB();
		if(!groupExistsName($newname) AND !isSystemGroup($this->info['people_group_id'])) {
			$awesome=mysql_query("UPDATE `people_group` SET `people_group_name`='" . mysql_real_escape_string($newname) . "' WHERE `people_group_id`='" . $this->info['people_group_id'] . "'");
			$this->info = groupInfo($this->info['people_group_id']);
			return true;
		}
		return false;
	}
	
	public function isInGroup($uid) {
		connectToDB();
		$awesome=mysql_query("SELECT * FROM `people_in_group` WHERE people_id='" . mysql_real_escape_string($uid) . "' AND people_group_id='" . $this->info['people_group_id'] . "'");
		return (mysql_num_rows($awesome)>0 ? true : false);
	}
	
	public function userList() {
		connectToDB();
		$awesome=mysql_query("SELECT people.people_id FROM `people` JOIN people_in_group ON people_in_group.people_id=people.people_id WHERE people_in_group.people_group_id='" . $this->info['people_group_id'] . "'");
		$return=array();
		for($i=0;$temp = mysql_fetch_array($awesome);$i++) {
			$return[$i]=$temp['people_id'];
		}
		return $return;
	}
	
	public function accessList() {
		connectToDB();
		$newdata=mysql_query("SELECT * FROM people_group_access WHERE people_group_id='" . $this->info['people_group_id'] . "'");
		$return=array();
		while($data = mysql_fetch_array($newdata)) {
			$return[$data['people_group_name']]=$data;
		}
		return $return;
	}
	
	public function isInNav_id($nid) {
		connectToDB();
		$awesome=mysql_query("SELECT * FROM `menu` WHERE `people_group_id`='" . $this->info['people_group_id'] . "' AND `page_id`='" . mysql_real_escape_string($nid) . "'");
		return (mysql_num_rows($awesome)>0 ? true : false);
	}
	
	public function addNav_id($nid) {
		connectToDB();
		if(!$this->isInNav_id($nid)) {
			mysql_query("INSERT INTO `menu` (`people_group_id`,`page_id`) VALUES ('" . $this->info['people_group_id'] . "','" . mysql_real_escape_string($nid) . "')");
		}
	}
	
	public function removeNav_id($nid) {
		connectToDB();
		if($this->isInNav_id($nid)) {
			mysql_query("DELETE FROM `menu` WHERE `people_group_id`='" . $this->info['people_group_id'] . "' AND `page_id`='" . mysql_real_escape_string($nid) . "'");
		}
	}
	
	public function changeAccess($page,$access) {
		connectToDB();
		mysql_query("DELETE FROM `people_group_access` WHERE page_id='" . mysql_real_escape_string($page) . "' AND people_group_id='" . $this->info['peopl_group_id'] . "'");
		if($access!="0" AND $access!="1") {
			return;
		}
		mysql_query("INSERT INTO `people_group_access` (`page_id`,`people_group_id`) VALUES ('" . mysql_real_escape_string($page) . "','" . $this->info['peopl_group_id'] . "')");
	}
	
}

